package com.aj.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.aj.databases.dao.UserDao;
import com.aj.databases.entry.User;
import com.aj.http.AsynchronizationHttpRequest.IHttpCallBack;
import com.aj.http.request.UpdateUserInfoRequest;
import com.aj.http.response.BaseRespond;
import com.aj.http.response.Common;
import com.aj.http.response.UpdateUserInfoResponse;
import com.aj.utils.Contants;
import com.aj.utils.StringUtils;

public class EditUserInfoActivity extends BaseActivity {

	public static final int NICKNAME_INT = 0x990001;
	public static final int NAME_INT = 0x990002;
	public static final int SIGN_INT = 0x990003;
	public static final String EDITTYPE_STRING = "";
	public static final String RESULT_STRING = "RESULT_STRING";
	private EditText info;
	private Button sure_button;

	private Integer editType = null;

	private UserDao userDao;
	private User user;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_edit_userinfo);
		initView();
	}

	private void initView() {
		editType = getIntent().getIntExtra(EDITTYPE_STRING, -1);
		user = (User) getIntent().getSerializableExtra(
				Contants.USERINFOFLAG_STRING);
		userDao = new UserDao(this);
		info = (EditText) findViewById(R.id.info);
		sure_button = (Button) findViewById(R.id.sure_button);
		ButtonClickEvent buttonClickEvent = new ButtonClickEvent();
		sure_button.setOnClickListener(buttonClickEvent);
		switch (editType) {
		case NICKNAME_INT:
			setActionBarTitle(R.string.edit_nickname_text);
			info.setHint((getString(R.string.edit_please_text) + getString(R.string.edit_nickname_text)));
			break;
		case NAME_INT:
			setActionBarTitle(getString(R.string.edit_name_text));
			info.setHint((getString(R.string.edit_please_text) + getString(R.string.edit_name_text)));
			break;
		case SIGN_INT:
			setActionBarTitle(getString(R.string.edit_sign_text));
			info.setHint((getString(R.string.edit_please_text) + getString(R.string.edit_sign_text)));
			break;
		default:
			setActionBarTitle(R.string.edit_title_text);
			break;
		}
	}

	class ButtonClickEvent implements OnClickListener {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.sure_button:
				if (StringUtils.isStringEmpty(info.getText().toString().trim())) {
					Toast.makeText(EditUserInfoActivity.this,
							getToastMessage(editType), Toast.LENGTH_LONG)
							.show();
					return;
				}
				showProgressDialog(getString(R.string.local_update_load_text));
				UpdateUserInfoRequest updateUserInfoRequest = new UpdateUserInfoRequest();
				switch (editType) {
				case NICKNAME_INT:
					user.setNickName(info.getText().toString().trim());
					break;
				case NAME_INT:
					user.setName(info.getText().toString().trim());
					break;
				case SIGN_INT:
					user.setSign(info.getText().toString().trim());
					break;
				default:
					break;
				}
				updateUserInfoRequest.excute(EditUserInfoActivity.this, user,
						new IHttpCallBack() {
							@Override
							public void success(String result) {
								try {
									UpdateUserInfoResponse response = new UpdateUserInfoResponse(
											new JSONObject(result));
									Common common = response.getCommon();
									if (common
											.getReturnCode()
											.equals(BaseRespond.SUCCESS_RESULT_CODE_STRING)) {
										userDao.updateUser(user);
										Toast.makeText(
												EditUserInfoActivity.this,
												getString(R.string.update_user_info_text),
												Toast.LENGTH_LONG).show();
										finishPage(user);
									} else {
										Toast.makeText(
												EditUserInfoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
								dimissProgressDialog();
							}

							@Override
							public void fail(String result) {
								try {
									UpdateUserInfoResponse response = new UpdateUserInfoResponse(
											new JSONObject(result));
									Common common = response.getCommon();
									if (common != null) {
										Toast.makeText(
												EditUserInfoActivity.this,
												common.getErrorMsg(),
												Toast.LENGTH_LONG).show();
									}
								} catch (JSONException e) {
									e.printStackTrace();
								}
								dimissProgressDialog();
							}
						});
				break;
			}

		}
	}

	private void finishPage(User user) {
		Intent intent = new Intent();
		intent.putExtra(RESULT_STRING, info.getText().toString());
		intent.putExtra(Contants.USERINFOFLAG_STRING, user);
		setResult(editType, intent);
		finish();
	}

	private String getToastMessage(int editType) {
		String s = "";
		switch (editType) {
		case NICKNAME_INT:
			s = (getString(R.string.edit_please_text) + getString(R.string.edit_nickname_text));
			break;
		case NAME_INT:
			s = (getString(R.string.edit_please_text) + getString(R.string.edit_name_text));
			break;
		case SIGN_INT:
			s = (getString(R.string.edit_please_text) + getString(R.string.edit_sign_text));
			break;
		default:
			break;
		}
		return s;
	}
}
